﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;

namespace Shop.Business.Domain.Orders.Logic
{
    public class ControllerRestrictionVisitor : IRestrictionVisitor
    {
        private IOperation operation;


        public IOperation Operation
        {
            get { return operation; }
            set { operation = value; }
        }

        #region IRestrictionVisitor Members

        public void Accept(RangeRestriction restriction)
        {
            if (restriction.Minimum != null)
            {
                if (restriction.Minimum.CompareTo(GetOperationValue()) > 0)
                {
                    string description = !StringUtils.IsBlankOrNull(restriction.MinimumMessage)
                                             ?
                                         restriction.MinimumMessage
                                             : "value less then permissible";

                    throw new RestrictionException(operation, description);
                }

            }

            if (restriction.Maxinmum != null)
            {
                if (restriction.Maxinmum.CompareTo(GetOperationValue()) < 0)
                {
                    string description = !StringUtils.IsBlankOrNull(restriction.MaxinmumMessage)
                                             ?
                                         restriction.MaxinmumMessage
                                             : "value more then permissible";

                    throw new RestrictionException(operation, description);
                }

            }
        }

        #endregion

        protected object GetOperationValue()
        {
            IValueHolder valueHolder = operation as IValueHolder;
            if (valueHolder != null)
                return valueHolder.Value;

            return null;
        }
    }
}
